package com.youmingxing.view.clipheadphoto.clip1;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.youmingxing.utils.DisplayUtil;
import com.youmingxing.utils.LogUtil;

public class ClipImageBorderView extends View {

    /**
     * 水平方向与View的边距
     */
    private int mHorizontalPadding;
    /**
     * 垂直方向与View的边距
     */
    private int mVerticalPadding;
    /**
     * 绘制的矩形的宽度
     */
    private int mWidth;

    private int mCornerhight;

    private int mhight;

    /**
     * 边框的宽度 单位dp
     */
    private int mBorderWidth = 15;

    private float movePos;

    private Paint mPaint, mPaintRect, mPaintMove;

    public ClipImageBorderView(Context context) {
        this(context, null);
    }

    public ClipImageBorderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClipImageBorderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mBorderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, getResources().getDisplayMetrics());
        mWidth = DisplayUtil.getScreenWidth();
        mCornerhight = DisplayUtil.getScreenWidth() * 9 / 16;
        mhight = DisplayUtil.getScreenWidth() * 9 / 16 + 90;
        //绘制白框
        mPaintRect = new Paint();
        mPaintRect.setARGB(255, 255, 255, 255);
        mPaintRect.setStyle(Style.FILL);

        // 绘制边框
//        mPaint = new Paint();
//        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setAntiAlias(true);
//        mPaint.setColor(Color.YELLOW);
//        mPaint.setStrokeWidth(mBorderWidth);


        // 绘制阴影
        mPaint = new Paint();
        mPaint.setARGB(180, 255, 255, 255); //  绿色
        mPaint.setStyle(Style.FILL);

        mPaintMove = new Paint();
        mPaintMove.setARGB(255, 255, 255, 255);
        mPaintMove.setStyle(Style.FILL);


    }

    float scale = -5;

    public void setmZoomImageView(float scale) {
        this.scale = scale;
    }

    private float tempPos = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 计算矩形区域的宽度
        tempPos = movePos;
        // 计算距离屏幕垂直边界 的边距
        // 绘制上面蒙板
        if (tempPos < 0 && -tempPos < 45) { //  往上滑动
            LogUtil.i("--------onDraw--1-", "down");

            canvas.drawRect(0, 0, mWidth, 45 + tempPos, mPaintMove);
            // 绘制上面蒙板
            canvas.drawRect(0, 45 + tempPos, mWidth, 90, mPaint);
            canvas.drawRect(0, getHeight() - 90 - 90, mWidth,
                    getHeight() - 90 - 45 + tempPos, mPaint);
            canvas.drawRect(0, getHeight() - 90 - 45 + tempPos,
                    mWidth, getHeight() - 90, mPaintMove);
        } else if (tempPos < 0 && -tempPos >= 45) { //  往上滑动
            LogUtil.i("--------onDraw--2-", "down");

            canvas.drawRect(0, 0, mWidth, 0, mPaintMove);
            // 绘制上面蒙板
            canvas.drawRect(0, 0, mWidth, 90, mPaint);
            canvas.drawRect(0, mhight, getWidth(), mhight + tempPos, mPaint);
            canvas.drawRect(0, mhight + tempPos, getWidth(), mhight + 180, mPaintMove);
        } else if (tempPos > 90 && tempPos < 180) {  //  往下滑
//            // 绘制上面蒙板
            LogUtil.i("--------onDraw--3-", "down");

            canvas.drawRect(0, 45 + tempPos, mWidth, 90, mPaint);
            canvas.drawRect(0, 0, mWidth, 90, mPaint);
            canvas.drawRect(0, mhight, getWidth(), mhight + tempPos, mPaint);
            canvas.drawRect(0, mhight + tempPos, getWidth(), mhight + 180, mPaintMove);
        } else if (tempPos >= 0 && tempPos <= 90) {  //  往下滑
//            canvas.drawRect(0, 0, mWidth,
//                    tempPos-45, mPaintMove);
//            // 绘制上面蒙板
            // 绘制上面蒙板
            LogUtil.i("--------onDraw--4-", "down");
            canvas.drawRect(0, 0, mWidth, 90, mPaint);
            if (scale == -5) {
                // 绘制左下角 left ,top,right,bottom
                canvas.drawRect(0, mhight - 30, getWidth(), mhight + 600, mPaint);
            } else {
                canvas.drawRect(0, mhight + 10, getWidth(), mhight + 600, mPaint);
            }
        } else if (tempPos >= 180) {  //  往下滑
            LogUtil.i("--------onDraw--5-", "down");

            canvas.drawRect(0, 0, mWidth, 90, mPaintMove);
            // 绘制上面蒙板
            canvas.drawRect(0, 0, getWidth(), 0, mPaint);
            canvas.drawRect(0, mhight + 180, getWidth(), mhight + 180, mPaintMove);
            canvas.drawRect(0, mhight, getWidth(), mhight + 400, mPaint);
        }

        // 绘制左上角
        canvas.drawRect(0, 90, 10, 100, mPaintRect);
        // 绘制右下角
        canvas.drawRect(mWidth - 10, 90, mWidth, 100, mPaintRect);
        if (scale == -5) {
            // 绘制左下角 left ,top,right,bottom
            canvas.drawRect(0, mCornerhight + 40, 10, mCornerhight + 50, mPaintRect);
            // 绘制右下角
            canvas.drawRect(mWidth - 10, mCornerhight + 40, mWidth, mCornerhight + 50, mPaintRect);
        } else {
            // 绘制左下角 left ,top,right,bottom
            canvas.drawRect(0, mCornerhight + 80, 10, mCornerhight + 90, mPaintRect);
            // 绘制右下角
            canvas.drawRect(mWidth - 10, mCornerhight + 80, mWidth, mCornerhight + 90, mPaintRect);
        }

        //绘制边框
//        canvas.drawRect(mHorizontalPadding, mVerticalPadding, mHorizontalPadding + mWidth, mVerticalPadding + mWidth, mPaint);
        canvas.drawRect(0, getHeight(), 0, getHeight() + 50, mPaint);

    }

    public void setHorizontalPadding(int mHorizontalPadding) {
        this.mHorizontalPadding = mHorizontalPadding;
    }


    public void reDraw(float movePos) {
        this.movePos = movePos;
        postInvalidate();
    }


}
